﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mighty.Common;

namespace Mighty.Mighty
{
    public partial class BettingForm : Form
    {
        public Betting betting = new Betting();

        GameInfo gameinfo;
        ActualStatus actualStatus;

        public BettingForm(GameInfo g, ActualStatus s)
        {
            InitializeComponent();

            gameinfo = g;
            actualStatus = s;
            SetDisplay();
        }

        private Betting FindMyBetting()
        {
            Betting preMyBetting = null;
            foreach (Betting b in gameinfo.bettingHistory) if (b.position == gameinfo.myInfo.position) preMyBetting = b;     
            return preMyBetting;
        }

        private void SetDisplay()
        {
            Betting preMyBetting = FindMyBetting();

            Betting maxBetting = null;
            foreach (Betting b in gameinfo.bettingHistory) if (!b.isPass) maxBetting = b;

            string msg = "";
            InitShapeRadio();

            if (actualStatus == ActualStatus.BettingTicEvent)
            {

                if (maxBetting == null)
                {
                    msg = String.Format("{0}님이 배팅하실 차례입니다", gameinfo.myInfo);
                    EnableBiddingRatio(13);
                }
                else
                {
                    msg = String.Format("{0}님이 {1} ({2})을 부르셨습니다", gameinfo.players[maxBetting.position], maxBetting.giruda, maxBetting.limit);
                    EnableBiddingRatio(maxBetting.limit+1);
                }

                if (preMyBetting != null)
                {
                    if (preMyBetting.giruda == Giruda.Clover) rShapeClover.Checked = true;
                    else if (preMyBetting.giruda == Giruda.Diamond) rShapeDiamond.Checked = true;
                    else if (preMyBetting.giruda == Giruda.Spade) rShapeSpade.Checked = true;
                    else if (preMyBetting.giruda == Giruda.Heart) rShapeHeart.Checked = true;
                    else if (preMyBetting.giruda == Giruda.NoGiruda) rShapeNogiruda.Checked = true;
                }
                else
                {
                    rShapeClover.Checked = true;
                }
            } 
            else if ( actualStatus == ActualStatus.LastBettingEvent ) 
            {
                if (preMyBetting.giruda == Giruda.Clover) rShapeClover.Checked = true;
                else if (preMyBetting.giruda == Giruda.Diamond) rShapeDiamond.Checked = true;
                else if (preMyBetting.giruda == Giruda.Spade) rShapeSpade.Checked = true;
                else if (preMyBetting.giruda == Giruda.Heart) rShapeHeart.Checked = true;
                else if (preMyBetting.giruda == Giruda.NoGiruda) rShapeNogiruda.Checked = true;
                DisableShapeRadio(preMyBetting.giruda);
                EnableBiddingRatio(maxBetting.limit + 2);

                bntPass.Enabled = false;
                msg = "새로운 공약정보를 입력하세요.";
            }

            lbState.Text = msg;
            btnBetting.Focus();
        }


        private void InitShapeRadio()
        {
            rShapeClover.Enabled = true;
            rShapeDiamond.Enabled = true;
            rShapeSpade.Enabled = true;
            rShapeHeart.Enabled = true;
            rShapeNogiruda.Enabled = true;
        }

        private void DisableShapeRadio(Giruda giruda)
        {
            if (giruda == Giruda.Clover) rShapeClover.Enabled = false;
            else if (giruda == Giruda.Diamond) rShapeDiamond.Enabled = false;
            else if (giruda == Giruda.Spade) rShapeSpade.Enabled = false;
            else if (giruda == Giruda.Heart) rShapeHeart.Enabled = false;
            else if (giruda == Giruda.NoGiruda) rShapeNogiruda.Enabled = false;
        }

        private void EnableBiddingRatio(int min)
        {
            rBid13.Enabled = true;
            rBid14.Enabled = true;
            rBid15.Enabled = true;
            rBid16.Enabled = true;
            rBid17.Enabled = true;
            rBid18.Enabled = true;
            rBid19.Enabled = true;
            rBid20.Enabled = true;

            if (min > 13) rBid13.Enabled = false;
            if (min > 14) rBid14.Enabled = false;
            if (min > 15) rBid15.Enabled = false;
            if (min > 16) rBid16.Enabled = false;
            if (min > 17) rBid17.Enabled = false;
            if (min > 18) rBid18.Enabled = false;
            if (min > 19) rBid19.Enabled = false;

            if (min == 13) rBid13.Checked = true;
            else if (min == 14) rBid14.Checked = true;
            else if (min == 15) rBid15.Checked = true;
            else if (min == 16) rBid16.Checked = true;
            else if (min == 17) rBid17.Checked = true;
            else if (min == 18) rBid18.Checked = true;
            else if (min == 19) rBid19.Checked = true;
            else if (min == 20) rBid20.Checked = true;
        }

        private int GetBiddingRatio()
        {
            if (rBid13.Checked == true) return 13;
            else if (rBid14.Checked == true) return 14;
            else if (rBid15.Checked == true) return 15;
            else if (rBid16.Checked == true) return 16;
            else if (rBid17.Checked == true) return 17;
            else if (rBid18.Checked == true) return 18;
            else if (rBid19.Checked == true) return 19;
            else if (rBid20.Checked == true) return 20;
            return -1;
        }

        private void btnBetting_Click(object sender, EventArgs e)
        {
            if (rShapeClover.Checked) betting.giruda = Giruda.Clover;
            else if (rShapeDiamond.Checked) betting.giruda = Giruda.Diamond;
            else if (rShapeSpade.Checked) betting.giruda = Giruda.Spade;
            else if (rShapeHeart.Checked) betting.giruda = Giruda.Heart;
            else if (rShapeNogiruda.Checked) betting.giruda = Giruda.NoGiruda;

            betting.isPass = false;
            betting.limit = GetBiddingRatio();

            this.Close();
        }

        private void bntPass_Click(object sender, EventArgs e)
        {

            betting.isPass = true;
            this.Close();
        }

        private void cmbShape_SelectedIndexChanged(object sender, EventArgs e)
        {

        }




    }
}
